/*
 * Copyright (c) 2006, 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx.controller;

import cn.webrx.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
 * <p>Project: smvc2022 - IndexController
 * <p>Powered by webrx On 2022-04-07 09:36:38
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
@RestController
@RequestMapping("/admin")
public class IndexController {
    @Autowired
    private JwtUtils ju;

    @Autowired
    private HttpServletRequest request;

    @RequestMapping("/login")
    public Map<String,String> login(String account, String password)    {
        Map<String,String> map = new HashMap<>();
        if("root".equalsIgnoreCase(account) && "root123".equals(password)){
            //账号密码正确，登录
            map.put("code","200");
            map.put("msg","账号登录成功");
            map.put("token",ju.getToken(null,30));
        }else{
            map.put("code","405");
            map.put("msg","登录失败");
        }
        return map;
    }

    @RequestMapping("/check")

    public String check() {
        return "check";
    }

    @RequestMapping("/add")
    public String add(@RequestHeader(name = "token",required = false,defaultValue = "") String token,HttpServletRequest req) {
        System.out.println(token);
        System.out.println(request.getHeader("token"));
        System.out.println(req.getMethod());
        if(req.getHeader("token") == null){
            return "你无权操作add";
        }else if(ju.verifyToken(req.getHeader("token"))){
            return "add";
        }else{
            return "你无权操作add";
        }
    }

    @RequestMapping("/del")
    public String del() {

        return "del....";
    }
}
